package com.qk.manangement.controller;

import cn.hutool.core.util.ObjectUtil;
import com.qk.common.context.UserContext;
import com.qk.common.properties.RedisProperties;
import com.qk.common.response.ClueByIdResponse;
import com.qk.common.response.PageResult;
import com.qk.common.response.Result;
import com.qk.entity.pojo.Clue;
import com.qk.entity.request.clue.ClueUpdateRequest;
import com.qk.entity.request.clue.FalseClueRequest;
import com.qk.manangement.annotation.ClearRedisCache;
import com.qk.manangement.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/clues")
public class ClueController {

    @Autowired
    private ClueService clueService;

    /**
     * 分页查询
     */
    @GetMapping
    public Result pageList(Integer clueId,String phone,Integer status,Integer channel,String assignName,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer pageSize
                           ) {
        PageResult pageResult = clueService.PageList(clueId, phone, status, channel, assignName, page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 添加线索
     */
    @PostMapping
    @ClearRedisCache(keys = {RedisProperties.REDIS_KEY_PREFIX+RedisProperties.REDIS_KEY_OVERVIEW})
    public Result add(@RequestBody Clue clue) {
        boolean add = clueService.add(clue);
        if (add) {
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    /**
     * 分配线索
     */
    @PutMapping("/assign/{clueId}/{userId}")
    @ClearRedisCache(keys = {RedisProperties.REDIS_KEY_PREFIX+RedisProperties.REDIS_KEY_OVERVIEW})
    public Result assign(@PathVariable Integer clueId, @PathVariable Integer userId) {
        boolean assign = clueService.assign(clueId, userId);
        if (assign) {
            return Result.success("分配成功");
        }
        return Result.error("分配失败");
    }

    /**
     * 伪线索标记
     */
    @PutMapping("/false/{id}")
    @ClearRedisCache(keys = {RedisProperties.REDIS_KEY_PREFIX+RedisProperties.REDIS_KEY_OVERVIEW})
    public Result falseClue(@PathVariable Integer id, @RequestBody FalseClueRequest falseClueRequest) {
        if (ObjectUtil.isNull(falseClueRequest.getReason())) return Result.error("请填写伪线索原因");
        boolean assign = clueService.falseClue(id,falseClueRequest);
        if (assign) {
            return Result.success("标记成功");
        }
        return Result.error("标记失败");
    }

    /**
     * 转商机处理
     */
    @PutMapping("/toBusiness/{id}")
    @ClearRedisCache(keys = {RedisProperties.REDIS_KEY_PREFIX+RedisProperties.REDIS_KEY_OVERVIEW})
    public Result toBusiness(@PathVariable Integer id) {
        boolean assign = clueService.toBusiness(id);
        if (assign) {
            return Result.success("转商机成功");
        }
        return Result.error("转商机失败");
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
       ClueByIdResponse  clue = clueService.getByIdMy(id);
        if (clue != null) {
            return Result.success(clue);
        }
        return Result.error("查询失败");
    }

    /**
     * 跟进线索
     */
    @PutMapping
    @ClearRedisCache(keys = {RedisProperties.REDIS_KEY_PREFIX+RedisProperties.REDIS_KEY_OVERVIEW})
    public Result update(@RequestBody ClueUpdateRequest clueUpdateRequest) {
        System.out.println(UserContext.getUserId());
        boolean update = clueService.updateClue(clueUpdateRequest);
        if (update) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    /**
     *  线索池列表查询
     */
    @GetMapping("/pool")
    public Result poolList(Integer clueId,String phone,Integer channel,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        PageResult pageResult = clueService.PagePoolList(clueId, phone, channel, page, pageSize);
        return Result.success(pageResult);
    }


}
